<template>
  <div class="app-container">
    <el-form :model="query" :inline="true">
      <el-form-item label="广告标题">
        <el-input v-model="query.title" placeholder=" "></el-input>
      </el-form-item>
      <el-form-item label="状态">
        <el-select v-model="query.status" filterable clearable>
          <el-option
            v-for="item in statusOptions"
            :label="item.name"
            :value="item.code"
            :key="item.code"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="queryData">查询</el-button>
        <el-button icon="el-icon-refresh" class="filter-item" @click="reload">重置</el-button>
        <el-button type="primary" icon="el-icon-circle-plus-outline" @click="openAdd">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="list" style="width: 100%" border stripe>
      <el-table-column label="序号" type="index" align="center" width="180"></el-table-column>
      <el-table-column align="center" prop="title" label="广告管理"></el-table-column>
      <el-table-column align="center" label="广告图片">
        <template slot-scope="scope">
          <el-image
            style="width: 90px; height: 60px"
            :src="scope.row.imageUrl"
            :preview-src-list="[scope.row.imageUrl]"
          ></el-image>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="advertUrl" label="广告链接"></el-table-column>
      <el-table-column align="center" prop="status" label="状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status==1" type="success">正常</el-tag>
          <el-tag v-if="scope.row.status==0" type="danger">禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="sort" label="排序"></el-table-column>
      <el-table-column align="left" label="操作">
        <template slot-scope="scope">
          <el-button type="success" size="small " @click="handleEdit(scope.row.id)">编辑</el-button>
          <el-button type="danger" size="small " @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="page.current"
      :page-sizes="[10, 20, 30, 50]"
      :page-size="page.size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="page.total"
    ></el-pagination>
    <edit
      :oldImageUrl="edit.oldImageUrl"
      :Visible="edit.visible"
      :title="edit.title"
      :formData="edit.formData"
      :remoteClose="remoteClose"
    ></edit>
  </div>
</template>
<script>
const statusOptions = [
  { code: 0, name: "禁用" },
  { code: 1, name: "正常" }
];
import advertApi from "@/api/advert";
import edit from "./edit";

export default {
  components: { edit },
  name: "Advert",
  data() {
    return {
      list: [],
      query: {},
      statusOptions,
      page: {
        size: 20,
        current: 1,
        total: 0
      },
      edit: {
        visible: false,
        title: "",
        // 修改前的照片url  编辑时 查询出来的图片
        // 如果点击编辑 点击上传图片 会把上一个图片删除 保留现在的 但是没有点击提交 不能吧这个图片和下面的文字绑定在一块
        // 所以保留下最初的图 防止丢失
        oldImageUrl: null,
        //点击编辑的时候根据编辑的id获取数据需要吧数据放在这里传过去
        formData: {
          imageUrl: null
        }
      }
    };
  },
  created() {
    this.fetchData();
  },
  methods: {
    // 关闭窗口
    remoteClose() {
      this.edit.formData = { imageUrl: null };
      this.edit.visible = false;
      this.fetchData();
    },
    fetchData() {
      advertApi
        .getList(this.query, this.page.size, this.page.current)
        .then(res => {
          console.log(res);
          this.list = res.data.records;
          this.page.total = res.data.total;
        });
    },
    handleCurrentChange(val) {
      this.page.current = val;
      this.fetchData();
    },
    handleSizeChange(val) {
      this.page.size = val;
      this.fetchData();
    },
    // c表单查询
    queryData() {
      //将页码变为第1页
      this.page.current = 1;
      this.fetchData();
    },
    // 重置
    reload() {
      this.query = {};
      this.fetchData();
    },
    openAdd() {
      this.edit.visible = true;
      this.edit.title = "新增";
    },
    handleEdit(id) {
      advertApi.getById(id).then(res => {
        if (res.code === 20000) {
          console.log(res, "查询id");
          this.edit.formData = res.data;
          this.edit.oldImageUrl = res.data.imageUrl; //修改前的照片url
        }
      });
      //弹出窗口
      this.edit.title = "编辑";
      //弹出窗口
      this.edit.visible = true;
    },
    handleDelete(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          advertApi.deleteById(id).then(res => {
            if (res.code == 20000) {
              this.$message({
                type: "success",
                message: "删除成功!"
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    }
  }
};
</script>